package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.security.auth.login.LoginException;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //employee数据插入员工表
        employeeMapper.insert(employee);
        //如果ids有数据,将数据迭代出来,存入中间表
        if (ids != null && ids.length > 0) {
            for (Long roleId : ids) {
                employeeMapper.insertRealation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee login(String username, String password) {
        if (!StringUtils.hasText(username)) {
            throw new LogicException("用户名不能为空");
        }
        if (!StringUtils.hasText(password)) {
            throw new LogicException("密码不能为空");
        }

        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            throw new LogicException("用户名或密码错误");
        }
        return employee;

    }

    @Override
    public Employee selectOne(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.deleteRelation(employee.getId());

        employeeMapper.updateByPrimaryKey(employee);

        if (ids != null && ids.length > 0) {
            for (Long roleId : ids) {
                employeeMapper.insertRealation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        /*int count = employeeMapper.selectForCount(qo);
        if(count == 0){
            return new PageResult(qo.getCurrentPage(),qo.getPageSize(),0,Collections.emptyList());
        }

        int totalPage = count%qo.getPageSize()== 0
                ? count/qo.getPageSize() : count/qo.getPageSize()+1;

        if(qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }*/
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        //return new PageResult(qo.getCurrentPage(),qo.getPageSize(),count,employees);
        return new PageInfo<Employee>(employees);
    }
}
